import * as Router from 'koa-router';
import visitRecoder from '../services/visitRecoderService';
import visitRecoderService from '../services/visitRecoderService';
import { IRouterContext } from 'koa-router';

//写拜访记录----保存拜访记录
let router = new Router();
router.prefix('/api/visitRecoder');
//该接口是保存拜访记录的数据
router.post('/saveVisitRecoder', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await visitRecoder.saveVisitRecoder(res);
    ctx.status = 200;
    ctx.body = json;
    await next()
});

//改接口是用于返回门店陈列函
router.post('/listLetter', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    let listLtetter = await visitRecoder.listLetter(res.storeName, res.userid);
    if (listLtetter) {
        ctx.status = 200;
        ctx.body = {
            islistLtetter: true,
            listLetter: listLtetter.listLetter
        };
    } else {
        ctx.body = {
            islistLtetter: false,
            listLetter: null
        };;
    };
    await next()
});

//该接口是根据id查询拜访记录的详情 
//id:拜访记录的_id   planType:拜访类型
router.post('/visitRecoderDetailById', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request;
    let res = rq.body;
    // console.log(res);
    let json = await visitRecoderService.getRecoderById(res.id, res.planType);
    // console.log('查询的拜访记录:' + json);
    if (json != null) {
        ctx.body = json;
        ctx.status = 200
    } else {
        ctx.body = null;
    }
    await next()
});





export default router